package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.service.UserInfoService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/doRegister")
    public String register(@RequestParam("username")String username, @RequestParam("password")String password, @RequestParam("type")String type, Model model) {
        List<User> users = userService.getAllUser();
        for(User u:users){
            if(username.equals(u.getUsername())){
                model.addAttribute("msg", "账号已存在");
                return "user-register";
            }
        }
        User user = new User(null, username, password, type, 0);
        userService.addUser(user);
        userInfoService.addUserInfo(username, type);
        model.addAttribute("msg", "注册成功");
        return "login";
    }

}
